local dongjin = fk.CreateSkill {
  name = "hanqing__dongjin",
}

Fk:loadTranslationTable{
  ["hanqing__dongjin"] = "东进",
  [":hanqing__dongjin"] = "出牌阶段限一次，你可以拼点，赢的角色摸两张牌并<a href='hanqing__alliance_href'>连横</a>，输的角色横置；"..
  "你拼点后，若你赢，且对方武将牌状态与你不同，你可以视为使用一张雷【杀】。",

  ["#hanqing__dongjin"] = "东进：与一名角色拼点，赢者摸两张牌并连横，输者横置",
  ["#hanqing__dongjin-slash"] = "东进：你可以视为使用雷【杀】",
}

local U = require "packages/hanqing/util"

dongjin:addEffect("active", {
  anim_type = "control",
  prompt = "#hanqing__dongjin",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(dongjin.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, dongjin.name)
    local winner = pindian.results[target].winner
    if winner then
      if not winner.dead then
        winner:drawCards(2, dongjin.name)
        if not winner.dead then
          U.setAlliance(winner, "horizontal")
        end
      end
      local loser = winner == player and target or player
      if not loser.dead and not loser.chained then
        loser:setChainState(true)
      end
    end
  end,
})

local function stateJudge(player, target)
  if player.chained and not target.chained then
    return false
  elseif target.chained and not player.chained then
    return false
  end
  if player.face_up and not target.face_up then
    return false
  elseif target.face_up and not player.face_up then
    return false
  end
  return U.getAlliance(player) == U.getAlliance(target)
end

dongjin:addEffect(fk.PindianFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(dongjin.name) and
      player:canUse(Fk:cloneCard("thunder__slash"), { bypass_times = true }) then
      if target == player then
        for to, v in pairs(data.results) do
          if v.winner == player and not stateJudge(player, to) then
            return true
          end
        end
      else
        return data.results[player] and data.results[player].winner == player and not stateJudge(player, target)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "thunder__slash",
      skill_name = dongjin.name,
      prompt = "#hanqing__dongjin-slash",
      cancelable = true,
      extra_data = {
        bypass_times = true,
        extraUse = true,
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).extra_data)
  end,
})

return dongjin
